﻿using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using Al_FrameWork;

[CanEditMultipleObjects]
[CustomEditor(typeof(LuaMyBehaviour))]
public class LuaMyBehaviourEditor : UnityEditor.Editor
{
    private SerializedProperty luaScript;
    private SerializedProperty luaScriptPath;
    private TextAsset selectObj;

    public override void OnInspectorGUI()
    {
        luaScriptPath = serializedObject.FindProperty("luaScriptPath");
        luaScript = serializedObject.FindProperty("luaScript");
        serializedObject.Update();
        GUILayout.Space(10);

        //selectObj = DragAreaGetObject.GetOjbect();
        selectObj = (TextAsset)EditorGUILayout.ObjectField("Select Lua File", selectObj, typeof(TextAsset), false);
        DrawDefaultInspector();
        //EditorGUILayout.PropertyField(luaScriptPath,new GUIContent("luaScriptPath"));
        //EditorGUILayout.PropertyField(injections, true);
        //EditorGUILayout.PropertyField(injectStrings,true);
        //EditorGUILayout.PropertyField(injectFloats, true);
        if (luaScript != null) selectObj = (TextAsset)luaScript.objectReferenceValue;
        if (selectObj != null)
        {
            var path = AssetDatabase.GetAssetPath(selectObj);
            var prefix = string.Format("Assets/{0}/{1}/", GameConfig.AssetsFolderName, GameConfig.LuaScriptsFolder);
            var suffix = ".lua.txt";
            if (path.IndexOf(prefix) != 0 || Path.GetExtension(path) != ".txt")
            {
                // GlobalFunctions.Err("select lua error");
            }
            else
            {
                path = path.Replace(prefix, string.Empty);
                path = path.Replace(suffix, string.Empty);
                luaScriptPath.stringValue = path;
            }
        }

        GUILayout.Space(10);

        //if (GUI.changed)
        //{
        //    EditorUtility.SetDirty(target);
        //}
        serializedObject.ApplyModifiedProperties();
    }
}